package domain.lexicalAnalyzerGenerator;

import java.util.List;
import java.util.Vector;

import domain.regularExpression.UnaryRegularExpressionOperator;

public class WhileInstruction extends Instruction {

	private String possibleSymbols;
	private List<Instruction> subInstructions;
	
	public WhileInstruction(UnaryRegularExpressionOperator unaryRegularExpressionOperator) 
	throws InstructionException{
		possibleSymbols = Instruction.getFirsts(unaryRegularExpressionOperator);
		subInstructions = new Vector<Instruction>();
		for (int i = 0; i < unaryRegularExpressionOperator.getSubElements().size(); i++){
			subInstructions.add
				(Instruction.create(unaryRegularExpressionOperator.getSubElements().get(i)));
		}
	}
	
	public String generateCode() throws CodeGenerationException {
		String res = " while(grammarStringHelper.belongsCurrentToken(\"" + possibleSymbols + "\")){\n";
		
		for (int i = 0; i < subInstructions.size(); i++){
			res += subInstructions.get(i).generateCode();
		}
		
		res += "}\n";
		
		return res;
	}
}
